<?php
/**
 * Created by PhpStorm.
 * User: sdjs-014
 * Date: 2017/8/20
 * Time: 11:59
 */

namespace app\common\controller;

use app\common\model\UserToken;
use app\common\model\AdminToken;
use think\Controller;
use think\facade\Cookie;


class BaseController extends Controller
{

    protected static $in  = null; //请求参数
    public static    $uid = null;

    public function __construct()
    {

        parent::__construct();

        if (MODULE === "adm") { //后台接口执行

            $router = [
                'adm/Admin/login'
            ];

            $at     = new AdminToken();
            $token  = Cookie::get("admToken");

            if(!$token) {
                if(!in_array(URL_PATH,$router))
                    rj(1,'登录失效，请重新登录');

            } else {
                self::$uid = $at->getUserId($token,true);

                if(!self::$uid) {
                    if(!in_array(URL_PATH,$router))
                        rj(1,'登录失效，请重新登录');
                }

                define("UID", self::$uid);
            }

        } else { //前台接口执行

            $ut = new UserToken();

            //令牌换用户ID
            $token = Cookie::get("token");
            if ($token) {
                self::$uid = $ut->getUserId($token);
            }

            define("UID", self::$uid);
        }

    }

    /**
     * 接收并校验参数,并将验证后的参数保存在self::$_i中
     * @author 王崇全
     * @param array $paramsInfo 参数列表 四项依次为:参数名,默认值,tp的强制转换类型(s,d,a,f,b),tp的验证规则
     *                          eg:[
     *                          [ "age",  18, "d",  "number|<=:150|>:0"],
     *                          [ "sex",  null, "s",  "require"],
     *                          ]
     * @return array|null
     */
    protected function I($paramsInfo)
    {
        //数据接收&校验
        $request = request();

        $toVali = false;
        $params = $rule = $field = [];
        foreach ($paramsInfo as $paramInfo) {
            $paramInfo[0] = $paramInfo[0] ?? null;
            $paramInfo[1] = $paramInfo[1] ?? null;
            $paramInfo[2] = $paramInfo[2] ?? null;
            $paramInfo[3] = $paramInfo[3] ?? null;

            if (!is_array($paramInfo) || !$paramInfo[0]) {
                continue;
            }

            //$parmInfo[0] 参数名
            $paramInfo[0] = trim($paramInfo[0], "|");
            $param        = explode("|", $paramInfo[0]);
            $paramName    = $param[0];

            //$parmInfo[2] tp的强制转换类型
            if (in_array($paramInfo[2], [
                "s",
                "d",
                "b",
                "a",
                "f",
            ])) {
                $param = "{$paramName}/{$paramInfo[2]}";
            }

            //$parmInfo[1] 默认值
            if (isset($paramInfo[1])) {
                $params[ $paramName ] = $request->param($param, $paramInfo[1]);
            } else {
                $params[ $paramName ] = $request->param($param);
            }

            //$parmInfo[3] tp的验证规则
            if (is_string($paramInfo[3])) {
                $rule[ $paramInfo[0] ] = $paramInfo[3];
                $toVali                = true;
            }

        }


        self::$in = $params;

        if (!$toVali) {
            return $params;
        }

        $vali = $this->validate(self::$in, $rule);
        if ($vali === true) {
            return self::$in;
        }

        $msg = "";
        foreach ($vali as $v) {
            $msg .= "；".$v;
        }
        $msg = mb_substr($msg, 1);

        //判断api
        if (strtoupper(MODULE) == "API" || strtoupper(MODULE) == "ADMIN") {
            rj(1, $msg);
        }
        rj(3,$msg);

        return null;
    }

    /**
     * 重写 验证数据 方法
     * @access protected
     * @param array        $data     数据
     * @param string|array $validate 验证器名或者验证规则数组
     * @param array        $message  提示信息
     * @param bool         $batch    是否批量验证
     * @param mixed        $callback 回调方法（闭包）
     * @return array|string|true
     */
    protected function validate($data, $validate, $message = [], $batch = true, $callback = null)
    {

        return parent::validate($data, $validate, $message, $batch, $callback);

    }


}